package com.infore.airsupervise.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.infore.airsupervise.R;
import com.infore.airsupervise.app.MsgConst;
import com.infore.airsupervise.bean.SearchContainE;
import com.infore.airsupervise.bean.UserE;
import com.infore.airsupervise.biz.impl.UserAndGridBizImpl;
import com.infore.airsupervise.http.OnServerResponseListener;
import com.infore.airsupervise.ui.widget.SearchContainView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * 公共的人员fragment
 * 作者： xiaotao
 * 创建时间： 2019/12/2
 */
public class PsonContactFragment extends BaseFragment {

    @BindView(R.id.pson_list)
    SearchContainView psonListView;
    private UserAndGridBizImpl userAndGridBiz;
    private List<SearchContainE> allPsonList = new ArrayList<>();


    private OnClickListener clickListener = null;

    public void setClickListener(OnClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public interface OnClickListener {
        void onItemClick(UserE userE);

        void onCancelClick();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_pson_selector, container, false);
        unbinder = ButterKnife.bind(this, view);
        userAndGridBiz = new UserAndGridBizImpl();
        initViews();
        List<UserE> userEList = UserE.getAllPson();
        if (null == userEList || userEList.size() <= 0) {
            getAllPsonData();
        } else {
            setPsonSearchList(userEList);
        }
        return view;
    }

    private void initViews() {
        //添加监听事件
        psonListView.setSearchContainListener(new SearchContainView.SearchContainItemListener() {
            @Override
            public void onItemClickListener(SearchContainE item) {
                if (clickListener != null) {
                    UserE userE = (UserE) item.getDate();
                    clickListener.onItemClick(userE);
                }
            }
        });
        psonListView.setSearchContainViewListener(new SearchContainView.SearchContainViewListener() {
            @Override
            public void onClickListener(View v) {
                if (clickListener != null) {
                    clickListener.onCancelClick();
                }
            }
        });
    }

    /**
     * 服务器 获取人员信息
     */
    private void getAllPsonData() {
        if (null == userAndGridBiz) {
            userAndGridBiz = new UserAndGridBizImpl();
        }
        //loading dialog
        final SweetAlertDialog dialog = new SweetAlertDialog(getContext(), SweetAlertDialog.PROGRESS_TYPE)
                .setTitleText(MsgConst.HARD_LOADING)
                .setContentText(MsgConst.PLEASE_WAITING);
        dialog.setCancelable(false);
        dialog.show();
        userAndGridBiz.getAllPsonRequest(UserE.getUserInfo().getUserId(),
                new OnServerResponseListener<List<UserE>>() {
                    @Override
                    public void onSucceed(List<UserE> result, String desc) {
                        //dialog 隐藏
                        dialog.dismiss();
                        UserE.saveAllPson(result);//保存本地
                        setPsonSearchList(result);//设置人员信息
                        psonListView.updata();
                    }

                    @Override
                    public void onFailed(int errorCode, String title, String desc) {
                        //dialog 错误 异常提示
                        dialog.setTitleText(title)
                                .setContentText(desc)
                                .setConfirmText(MsgConst.I_SEE)
                                .changeAlertType(SweetAlertDialog.WARNING_TYPE);
                    }
                });
    }

    /**
     * 设置人员信息
     *
     * @param userEList
     */
    private void setPsonSearchList(List<UserE> userEList) {

        for (UserE userE : userEList) {
            allPsonList.add(new SearchContainE<>(userE, userE.getRealName(), ""));
        }
        psonListView.setItemData(allPsonList);
        psonListView.updata();
    }

}
